<?php
/*
*--------------------------------------------------------------
* Mysql 驱动类
*--------------------------------------------------------------
* 最后修改时间 2011-4-13 Leon
* @version 1.0
* @author Leon(tmkook@gmail.com)
* @date 2010-2-27
*--------------------------------------------------------------
*/
class mysql extends db{

	//连接数据库
	function connect($host,$user,$pass,$debug=TRUE,$pconn=FALSE){
		if($pconn){
			$conn =  @mysql_pconnect($host,$user,$pass);
		}else{
			$conn =  @mysql_connect($host,$user,$pass);
		}
		$this->debug = $debug;
		return $this->connect_id = $conn;
	}

    function selectDb($dbname){
        if( ! mysql_select_db($dbname,$this->connect_id)){
			$this->debug();
		}
    }

	//查询
	function query($sql){
        $this->result_id = NULL;
		$query = mysql_query($sql,$this->connect_id);
		if($query){
			$this->result_id = $query;
			return true;
		}
        $this->sql = $sql;
        $this->debug();
	}

	function fetch($result_type=''){
		if(!is_resource($this->result_id)) return array();
		switch($result_type){
			case 'object':
				$result = mysql_fetch_object($this->result_id);
				break;
			case 'array':
				$result = mysql_fetch_array($this->result_id);
				break;
			default:
				$result = mysql_fetch_assoc($this->result_id);
				break;
		}
       return $result;
	}

	function getOne($key=0){
		if(!is_resource($this->result_id)) return NULL;
		$result = mysql_fetch_array($this->result_id);
		return $result[$key];
	}

	function numRows(){
		if(!is_resource($this->result_id)) return NULL;
		return mysql_num_rows($this->result_id);
	}

	function insertId(){
		if(!is_resource($this->connect_id)) return NULL;
		return mysql_insert_id($this->connect_id);
	}

	function affectedRows(){
		if(!$this->connect_id) return NULL;
			return mysql_affected_rows($this->connect_id);
	}

	function freeResult(){
		if(is_resource($this->result_id))
			mysql_free_result($this->result_id);
	}

	function close(){
		if(is_resource($this->connect_id))
			mysql_close($this->connect_id);
	}

	function error(){
		return mysql_error();
	}
}